import {get_w_h, hideModal, isPreventScroll, setConfirm} from "./domFn";
import {getDelIndex} from "./util";
import {fn} from './index';
import {clickInitModal, isReverseModalArrow} from './modalScroll';

const doc = document;
const win = window;
const layer = doc.getElementById('layer');
const cancel = layer.getElementsByClassName('cancel')[0]
const add = layer.getElementsByClassName('add')[0]
const input = layer.getElementsByClassName('input')[0]
const iconPlus = doc.getElementsByClassName('add-item')
const modalDom = layer.getElementsByClassName('modal-box')[0]
const agentDom = doc.getElementsByClassName('item-list')
let clickAddNum;

win.onload = () => {
  cancel.onclick = () => {
    hideModal(layer)
  }
  add.onclick = () => {
    const {value} = input;
    const arr = value ? value.trim().split(',') : [];
    hideModal(layer)
    fn.render(clickAddNum, arr).add()
  }
  for (let i = 0; i < agentDom.length; i++) {
    agentDom[i].onclick = function (e) {
      let index = getDelIndex(this, e.target, 'icon-trash')
      index !== -1 && fn.render({
        parentIndex: i,
        delTargetIndex: index
      }).del()
    }
  }

  for (let i = 0; i < iconPlus.length; i++) {
    iconPlus[i].onclick = function () {
      clickInitModal(this);
      isReverseModalArrow(this, top, '', false)
      win.onresize = () => {
        const {top, left} = get_w_h(this);
        setConfirm(modalDom, top, left)
      }
      win.onscroll = () => {
        const {top, left} = get_w_h(this);
        isReverseModalArrow(this, top, left, true)

      }
      clickAddNum = i;
    }
  }
}

